package org.hxyjs.service.impl;

import org.hxyjs.dao.ChatRoomDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.ChatRoom;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.ChatRoomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("chatroomService")
public class ChatRoomServiceImpl implements ChatRoomService {
    @Resource
    ChatRoomDao chatroomDao;

    @Override
    public DTO<ChatRoom> getAllChatRooms() throws Exception {
        List<ChatRoom> chatrooms = chatroomDao.getAllChatRooms();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(chatrooms).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<ChatRoom>(messageEnum.getCode(), messageEnum.getMessage(), chatrooms);
    }

    @Override
    public DTO<ChatRoom> getChatRoomByUno(String uno) throws Exception {
        ChatRoom chatroom = chatroomDao.getChatRoomByUno(uno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(uno).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<ChatRoom>(messageEnum.getCode(), messageEnum.getMessage(), chatroom);
    }

    @Override
    public DTO<ChatRoom> deleteChatRoom(String uno) throws Exception {
        Integer result = chatroomDao.deleteChatRoom(uno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.DELETEADMINSUCCESS : MessageEnum.DELETEADMINFAIL;
        return new DTO<ChatRoom>(messageEnum.getCode(), messageEnum.getMessage());
    }

    @Override
    public DTO<ChatRoom> updateChatRoom(ChatRoom chatroom) throws Exception {
        Integer result = chatroomDao.updateChatRoom(chatroom);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
        return new DTO<ChatRoom>(messageEnum.getCode(), messageEnum.getMessage());
    }

    @Override
    public DTO<ChatRoom> addChatRoomByUno(ChatRoom chatroom) throws Exception {
//        chatroom.setUstate("未激活");
        Integer result = chatroomDao.addChatRoomByUno(chatroom);
//
//        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.ADDADMINSUCCESS : MessageEnum.ADDADMINFAIL;
        return new DTO<ChatRoom>(messageEnum.getCode(), messageEnum.getMessage(), chatroom);
    }

    @Override
    public DTO<ChatRoom> getChatRoomsByPage(PageInfo pageInfo) throws Exception {
        List<ChatRoom> chatrooms = chatroomDao.getChatRoomsByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(chatrooms).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<ChatRoom>(messageEnum.getCode(), messageEnum.getMessage(), chatrooms, pageInfo);
    }

    @Override
    public DTO<ChatRoom> getChatRoomsWithOrders(String uno) throws Exception {
        return null;
    }

    /**
     * 查询MongoDB
     *
     * @param uno
     * @return
     * @throws Exception
     */

    @Override
    public DTO<ChatRoom> activate(String uno) throws Exception {
//        ChatRoom chatroom = new ChatRoom();
//        chatroom.setUno(uno);
//        chatroom.setUstate("已激活");
//        Integer result = chatroomDao.updateChatRoom(chatroom);
//        //isPresent表示判断不为空成立则dto为？与：之间的对象
//        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
//        return new DTO<ChatRoom>(messageEnum.getCode(), messageEnum.getMessage());
        return null;
    }

    @Override
    public DTO<ChatRoom> lettory(String uno,String giftName) throws Exception {
        ChatRoom chatroom = chatroomDao.getChatRoomByUno(uno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(uno).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<ChatRoom>(messageEnum.getCode(), messageEnum.getMessage(), chatroom,giftName);
    }


//    @Override
//    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
//        DTO<Account> dto = new DTO<Account>();
////        Optional<Account> adminOP = Optional.ofNullable(chatroomDao.getAccountByAccountName(accountName));
////        dto.setT(adminOP.get());
////        MessageEnum messageEnum = adminOP.isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
////        dto.setCode(messageEnum.getCode());
////        dto.setMsg(messageEnum.getMessage());
//        return dto;
//    }
}
